var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var md5 = require("../../utils/md5.js");
const Upyun = require('../../utils/upyun-wxapp-sdk');
var phone = '';
var wxAuthFail = true;
var inviteuid = '';

Page({
  data: {
      util: util,
      minHeight: 0,
      userInfo:{},


      imgWH: 0,

      bottom_nav_data: [],
      isIphone_x: false,
      nav_bgColor: '#ffffff',
      items: [
      { name: '先生', value: '先生', checked: 'true'},
      { name: '女士', value: '女士' },
      ],
      swiperImgUrls2: [],//轮播图2
  },
  loadMore:function(){
      if(!noMore){
        page ++;
        this.load();
      }
  },
  onLoad: function(data){
    inviteuid = data.inviteuid ? data.inviteuid : ''; 
    this.getLoginInfo(inviteuid);
    var that = this;
    var uid = data.uid;

    var tc = app.get_tc();//简体繁体转换
    that.setData({
      tc: tc
    })
 
    var titlename = tc == 1 ? '尊享訂製' : '尊享订制';
    
    wx.setNavigationBarTitle({
      title: titlename,
    }); 

    //获取颜色配置
    app.getColorSetData(function (color) {
      console.log("color=====",color);
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('liuyan/liuyan', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });



    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          minHeight: res.windowHeight - 45,
          imgWH: (res.windowWidth - 18) / 4 - 12
        });
      }
    });


    //读取幻灯片图片链接2
    that.loadSwiperImage(10);

  },

  getLoginInfo: function (inviteuid) {
    var that = this;
    app.LoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        token: res.token
      });

      var id = inviteuid ? inviteuid : res.uid;
      util.api({
        data: {
          "uid": id,
          "publickey": util.publiccode,
          "ac": "userinfo",
          //"tc": that.data.tc
        },
        success: function (responseData) {
          console.log("用户信息", responseData.data);
          that.setData({
            userinfo: responseData.data
          });
        }
      });

    });
  },



  //重新读取 登录用户信息
  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      that.getLoginInfo();
    }
  },
  goAtherPage: function (e) {
    app.saveFormId(e.detail.formId);
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },


  //保存formId
  submit: function (e) {
    // app.saveFormId(e.detail.formId);
    console.log(e);
    var gender = e.detail.value.gender;
    var uname = e.detail.value.name;
    var phone = e.detail.value.mobile;
    var message = e.detail.value.desc;
    if(uname == ""){
      var contentname = this.data.tc == 1 ? '聯絡人不能為空' : '联络人不能为空';
       wx.showModal({
            title: '提示',
            content: contentname,
            showCancel: false,
          })
      return; 
    }else if(phone == ""){
      var contentmobile = this.data.tc == 1 ? '聯絡電話不能為空' : '联络电话不能为空';
       wx.showModal({
            title: '提示',
            content: contentmobile,
            showCancel: false,
          })
      return;
    }

    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'postliuyan' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "postliuyan",
        uid: that.data.uid,
        sex : gender,
        uname: uname,
        phone: phone,
        message: message,
        orgid:util.orgid,
        privatekey: privatekey
      },
      success: function (responseData) {
        console.log(responseData);
        if(responseData.data.res == true){
          var contentname = this.data.tc == 1 ? '提交成功' : '提交成功';
          app.dialogToast(contentname, "success", function () {
            // wx.reLaunch({ url: '/pages/liuyan-center/liuyan-center' });
          })
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'success'
          })
        }
         
      }
    });
  },

  submitFormId: function (e) {
    // app.saveFormId(e.detail.formId);
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

   loadSwiperImage: function(index){
    var that = this;
    var params = {
      "publickey": util.publiccode,
      "ac": "indexslide",
      "uid": util.orgid,
      "block": index,
      "tc": that.data.tc
    };
    util.api({
      data: params,
      success: function (responseData) {
        console.log(index + "-- swiper image:", responseData);
        var result = responseData.data;
        if (result && result.length > 0) {
          if (index == 2) {
            result.forEach(function (m, i) {
              var col = parseInt(m.col);
              col = col == 0 ? 1 : col;
              result[i].img_w = screen_w / col;
              console.log("顶图宽：" + m.img_w + "; col = " + col);
            });
            that.setData({
              logo_urls: result
            })

            that.logo_urlsTC();
          } else if (index == 3) {
            var colCount = result[0].col == '0' ? 4 : parseInt(result[0].col);
            var topItem_w = (screen_w - 3) / colCount;
        
            that.setData({
              top_menu_item_w: topItem_w,
              topmenuInfos: result,
              top_menu_bg_color: result[0].bgcolor ? result[0].bgcolor : '#fafafa',
            });
      
            // that.topmenuInfosTC();
            console.log('that.data.topmenuInfos------------->>>>', that.data.topmenuInfos)
          } else if(index == 1){
            that.setData({
              swiperImgUrls: result
            });
          } else if (index == 10) {
            that.setData({
              swiperImgUrls2: result
            });
          } 
          else if (index == 11) {
            that.setData({
              swiperImgUrls3: result
            });
          }
            else if (index == 4) {
            var item_data = result[0];
            var colCount = item_data.col == '0' ? 1 : parseInt(item_data.col);
            var w_h = (screen_w - (colCount + 1) * 5) / colCount;
            item_data.w_h = w_h;
            that.setData({
              jingxuanOb: item_data
            })
            console.log("精选课程", item_data)
            that.loadGoodsListData(0, 1);
          } else if (index == 5) {
            var item_data = result[0];
            var colCount = item_data.col == '0' ? 1 : parseInt(item_data.col);
            var w_h = (screen_w - (colCount + 1) * 7) / colCount;
            item_data.w_h = w_h;
            that.setData({
              tuijianOb: item_data
            })
            that.loadGoodsListData(1, 0);
            if(util.orgid=='17758'){
              that.setData({
                tuijianOb: that.splitNavtitle(result),
                tuijianData: that.splitNavtitle(result)
              })
            }
          } else if (index == 6) {
            var item_data = result[0];
            var colCount = item_data.col == '0' ? 1 : parseInt(item_data.col);
            var w_h = (screen_w - (colCount + 1) * 7) / colCount;
            item_data.w_h = w_h;
            that.setData({
              xinpinOb: item_data
            })
            that.loadGoodsListData(0, 0);
          } else if (index == 7) {
            var item_data = result[0];
            var colCount = item_data.col == '0' ? 1 : parseInt(item_data.col);
            var w_h = (screen_w - (colCount + 1) * 7) / colCount;
            item_data.w_h = w_h;
            that.setData({
              fenleiOb: item_data
            })
            //读取商品分类数据
            that.getGoodsClassly();
          } else if (index == 8) {//门店
            that.setData({
              shopViewName: result[0].navtitle,
              mendian_data: result
            });
            console.log('mendian:', result)
          } else if (index == 11) {
            result.forEach(function (m, i) {
              var col = parseInt(m.col);
              col = col == 0 ? 1 : col;
              result[i].img_w = screen_w / col;
              console.log("贴图宽：" + m.img_w + "; col = " + col);
            });
            that.setData({
              tietu_urls: result
            })
            if (util.orgid == '17758') {
              that.setData({
                tietu_urls: that.splitNavtitle(result)
              })
            }
          }else if (index == 12) {
            console.log("index == 12",result);
            result.forEach(function (m, i) {
              var col = parseInt(m.col);
              col = col == 0 ? 1 : col;
              result[i].img_w = screen_w / col;
              console.log("贴图宽：" + m.img_w + "; col = " + col);
            });

            that.setData({
              zixunfenlei: result,
            })

            if (util.orgid == '17758') {
              that.setData({
                zixunfenlei: that.splitNavtitle(result),
              });
            }

            console.log("zixunfenlei", that.data.zixunfenlei);
          } else if (index == 13) {
            that.setData({
              gonggaoList: result
            })
            console.log("13=============",that.data.gonggaoList);
          }
        }
        wx.hideLoading();
      }
    });
  },

})